n = 19

var isHappy = function (n) {
    function getPow(n) {
        let sum = 0
        while (n !== 0) {
            sum += (n % 10) * (n % 10)
            n = Math.floor(n / 10)
        }
        return sum
    }

    let slow = n
    let fast = getPow(n)

    while (fast !== 1 && fast !== slow) {
        slow = getPow(slow)
        fast = getPow(getPow(fast))
    }
    return fast === 1
};

console.log(isHappy(n));
